<template>
  <div class="login-container">
    <div class="login-form-big">
      <img src="@/assets/lkd/logo.png" alt="" class="login-logo" />
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <el-form-item prop="loginName">
          <i class="el-icon-mobile-phone" />
          <el-input v-model="loginForm.loginName" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <i class="el-icon-lock" />
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            :type="type"
          />
          <span class="svg-container">
            <i
              class="el-icon-view"
              v-if="type === 'text'"
              icon-class="eye-open"
              @click="type = 'password'"
            />
            <i class="iconfont icon-biyanjing" v-else @click="type = 'text'" />
          </span>
        </el-form-item>
        <el-form-item prop="code">
          <span class="iconfont icon-dunpaibaoxianrenzheng_o" />
          <el-input v-model="loginForm.code" placeholder="请输入验证码" />
          <div class="securitycode" @click="clickImgFn">
            <img :src="imgSrc" alt="" />
          </div>
        </el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          class="loginBtn"
          style="width: 100%; margin-bottom: 30px"
          @click="Login"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { getImgCode } from '@/api/user'
export default {
  name: 'LoginVue',
  data () {
    return {
      type: 'password',
      loading: false,
      imgSrc: '',
      loginForm: {
        loginName: '',
        password: '',
        code: '',
        clientToken: '',
        loginType: 0
      },
      rules: {
        loginName: [
          { required: true, message: '请填写账号', trigger: 'blur' },
          {
            pattern: /^\w{3,12}$/,
            message: '账号格式不正确',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请填写密码', trigger: 'blur' }
        ],
        code: [
          // 1.效验规则  2.效验的值  3.回调函数(直接调用，表示通过效验)
          // callback(new Error('请你勾选用户协议') 没有通过效验
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('请输入验证码'))
            }
          }
        ]
      }
    }
  },
  mounted () {
    this.clickImgFn()
  },
  created () {
    console.log(process.env)
  },
  methods: {
    randomFn () {
      return Date.now()
    },
    async clickImgFn () {
      this.loginForm.clientToken = this.randomFn()
      const res = await getImgCode(this.loginForm.clientToken)
      // console.log(window.URL.createObjectURL(res))
      this.imgSrc = window.URL.createObjectURL(res)
      // console.log(this.imgSrc)
    },
    async Login () {
      await this.$refs.loginForm.validate()// 不传参数  返回一个错误promise实例
      // console.log('去登录')
      await this.$store.dispatch('user/login', this.loginForm)
      this.$router.push('/')
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #999; // 将输入框颜色改成蓝色
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url("~@/assets/lkd/background.png"); // 设置背景图片
  background-repeat: no-repeat;
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    .el-form-item__content {
      position: relative;
      .svg-container {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: #889aa4;
        .icon-biyanjing {
          width: 16px;
          height: 40px;
          padding: unset;
        }
      }
    }
    height: 52px;
    border: 1px solid #e2e2e2;
    // background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    background: #fff;
    border-radius: 5px;
    color: #454545;
    .el-form-item__error {
      color: red;
    }
  }
  .loginBtn {
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    opacity: 0.91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100vh;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form-big {
    position: relative;
    width: 518px;
    height: 388px;
    background: #fff;
    border-radius: 10px;
    padding: 76px 35px 0;
    box-sizing: border-box;
    .login-logo {
      position: absolute;
      width: 96px;
      height: 96px;
      top: -46px;
      left: 50%;
      margin-left: -48px;
    }
  }
  .login-form {
    position: relative;
    width: 448px;
    height: 280px;
    margin: 0 auto;
    overflow: hidden;
  }
  .iconfont,
  .el-icon-mobile-phone,
  .el-icon-lock {
    padding: 6px 5px 6px 15px;
    font-size: 18px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.securitycode {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 124px;
  height: 48px;
  overflow: hidden;
  img {
    width: 100%;
  }
}
</style>
